//
//  ScanZhuangXiangVC.m
//  ReceiptDelivery
//
//  Created by 吕晶 on 2020/11/12.
//  Copyright © 2020 吕晶. All rights reserved.
//

#import "ScanZhuangXiangVC.h"
#import "ScanZhuangXiangModel.h"
#import "ScanZhuangXiangCell.h"
#import "ScanZhuangXiangGoodCell.h"
#import "ScanZhuangXiangBangHuoVC.h"

@interface ScanZhuangXiangVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UIView *bottomView;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) ScanZhuangXiangModel *model;

@end

@implementation ScanZhuangXiangVC

-(UIView *)bottomView {
    if (!_bottomView) {
        _bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenH - BottomHeight - NaviHeight - 64, ScreenW, 64+BottomHeight)];
        _bottomView.backgroundColor = HexColor(0xffffff);
    }
    return _bottomView;
}

-(UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH - self.bottomView.nim_height - NaviHeight)];
        _tableView.backgroundColor = Color_backgroundColor;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        __weak_self;
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [weakself requestForData];
        }];
        _tableView.ly_emptyView = [LYEmptyView emptyViewWithImageStr:@"img_occupy"
             titleStr:@""
            detailStr:@""];
    }
    return _tableView;
}

-(ScanZhuangXiangModel *)model {
    if (!_model) {
        _model = [ScanZhuangXiangModel new];
    }
    return _model;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"扫一扫";
    [self.view addSubview:self.bottomView];
    [self.view addSubview:self.tableView];
    [self requestForData];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(requestForData) name:ReceiptDeliveryRefreshScanZhuangXiang object:nil];
}

- (void)bangHuoClick {
    ScanZhuangXiangBangHuoVC *vc = [ScanZhuangXiangBangHuoVC new];
    vc.model = self.model;
    [self.rt_navigationController pushViewController:vc animated:YES];
}

- (void)jieChuClick {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"确定解除货物绑定吗？" preferredStyle:UIAlertControllerStyleAlert];
    __weak_self;
    UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [weakself requestForJieChu];
    }];
    UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alert addAction:sureAction];
    [alert addAction:cancleAction];
    [self presentViewController:alert animated:YES completion:nil];
}

- (void)requestForJieChu {
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    if (self.model.rv_num.length) {
        //周转箱
        [dic setObject:self.model.rv_num forKey:@"rv_num"];
    }else {
        //普通包装
        [dic setObject:self.model.pk_id forKey:@"pk_id"];
        [dic setObject:self.model.pk_num forKey:@"pk_num"];
    }
    
    [HttpRequest PostWithApi:self.model.rv_num.length ? ReceiptDeliveryScanZhuangXiangZhouZhuanJieChu : ReceiptDeliveryScanZhuangXiangPuTongJieChu params:dic progress:^(NSProgress * _Nonnull progress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull operation, id  _Nonnull responseObject, BOOL code, NSInteger errorCode) {
        if (code) {
            [MBProgressHUD leShowSuccessHUDAddedTo:self.view message:@"解除成功"];
            [self requestForData];
        }
    } fail:^(NSError * _Nonnull error) {
        
    }];
}

- (void)editClick {
    ScanZhuangXiangBangHuoVC *vc = [ScanZhuangXiangBangHuoVC new];
    vc.model = self.model;
    [self.rt_navigationController pushViewController:vc animated:YES];
}

- (void)requestForData {
    if ([self.boxModel.type isEqualToString:@"revolve"]) {
        //获取周转箱信息
        [HttpRequest PostWithApi:ReceiptDeliveryScanZhuangXiangZhouZhuanInfo params:@{@"rv_num":self.boxModel.num} progress:^(NSProgress * _Nonnull progress) {
                    
        } success:^(NSURLSessionDataTask * _Nonnull operation, id  _Nonnull responseObject, BOOL code, NSInteger errorCode) {
            if (code) {
                self.model = [ScanZhuangXiangModel mj_objectWithKeyValues:responseObject];
                [self.tableView reloadData];
                [self configUI];
            }
            [self.tableView.mj_header endRefreshing];
        } fail:^(NSError * _Nonnull error) {
            [self.tableView.mj_header endRefreshing];
        }];
    }else {
        //获取普通包装信息
        [HttpRequest PostWithApi:ReceiptDeliveryScanZhuangXiangPuTongInfo params:@{@"pk_num":self.boxModel.num,@"pk_id":self.boxModel.pk_id} progress:^(NSProgress * _Nonnull progress) {
                    
        } success:^(NSURLSessionDataTask * _Nonnull operation, id  _Nonnull responseObject, BOOL code, NSInteger errorCode) {
            if (code) {
                self.model = [ScanZhuangXiangModel mj_objectWithKeyValues:responseObject];
                [self.tableView reloadData];
                [self configUI];
            }
            [self.tableView.mj_header endRefreshing];
        } fail:^(NSError * _Nonnull error) {
            [self.tableView.mj_header endRefreshing];
        }];
    }
}

- (void)configUI {
    if (self.model.goods.has_goods) {
        [self.bottomView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
        UIButton *jieChuBtn = [[UIButton alloc] initWithFrame:CGRectMake(30, 10, self.bottomView.nim_width - 60 - 92 - 15, 44)];
        [jieChuBtn setBackgroundColor:HexColorA(0xFBA962, 0.1)];
        jieChuBtn.layer.masksToBounds = YES;
        jieChuBtn.layer.cornerRadius = 6;
        [jieChuBtn setTitle:@"解除货物绑定" forState:0];
        [jieChuBtn setTitleColor:HexColor(0xFBA962) forState:0];
        jieChuBtn.titleLabel.font = BoldFontOfSize(16);
        [jieChuBtn addTarget:self action:@selector(jieChuClick) forControlEvents:UIControlEventTouchUpInside];
        [self.bottomView addSubview:jieChuBtn];
        
        UIButton *eidtBtn = [[UIButton alloc] initWithFrame:CGRectMake(jieChuBtn.nim_right + 15, 10, 92, 44)];
        [eidtBtn setBackgroundColor:HexColorA(0x3D68EB, 0.1)];
        eidtBtn.layer.masksToBounds = YES;
        eidtBtn.layer.cornerRadius = 6;
        [eidtBtn setTitle:@"编辑" forState:0];
        [eidtBtn setTitleColor:HexColor(0x3D68EB) forState:0];
        eidtBtn.titleLabel.font = BoldFontOfSize(16);
        [eidtBtn addTarget:self action:@selector(editClick) forControlEvents:UIControlEventTouchUpInside];
        [self.bottomView addSubview:eidtBtn];
    }else {
        [self.bottomView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
        UIButton *nextBtn = [[UIButton alloc] initWithFrame:CGRectMake(30, 10, self.bottomView.nim_width - 60, 44)];
        [nextBtn setBackgroundColor:Color_ThemeColor];
        nextBtn.layer.masksToBounds = YES;
        nextBtn.layer.cornerRadius = 6;
        [nextBtn setTitle:@"绑货" forState:0];
        [nextBtn setTitleColor:HexColor(0xffffff) forState:0];
        nextBtn.titleLabel.font = BoldFontOfSize(16);
        [nextBtn addTarget:self action:@selector(bangHuoClick) forControlEvents:UIControlEventTouchUpInside];
        [self.bottomView addSubview:nextBtn];
    }
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 2;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        ScanZhuangXiangCell *cell = [ScanZhuangXiangCell ScanZhuangXiangCellWithTableView:tableView];
        cell.model = self.model;
        return cell;
    }
    if (self.model.goods.has_goods) {
        ScanZhuangXiangGoodCell *cell = [ScanZhuangXiangGoodCell ScanZhuangXiangGoodCellWithTableView:tableView];
        cell.model = self.model.goods.info;
        return cell;
    }else {
        static NSString *cellID = @"EmptyBoxCell";
        UITableViewCell *cell2 = [tableView dequeueReusableCellWithIdentifier:cellID];
        if (cell2 == nil) {
            cell2 = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        }
        cell2.backgroundColor = Color_backgroundColor;
        cell2.selectionStyle = UITableViewCellSelectionStyleNone;
        UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(0, 110, ScreenW, 17)];
        lab.text = @"箱子是空的，去装点货吧~";
        lab.textColor = HexColor(0xBDBED9);
        lab.font = [UIFont systemFontOfSize:16];
        lab.textAlignment = NSTextAlignmentCenter;
        [cell2.contentView addSubview:lab];
        return cell2;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        ScanZhuangXiangCell *cell = [ScanZhuangXiangCell ScanZhuangXiangCellWithTableView:tableView];
        return cell.height;
    }
    if (self.model.goods.has_goods) {
        ScanZhuangXiangGoodCell *cell = [ScanZhuangXiangGoodCell ScanZhuangXiangGoodCellWithTableView:tableView];
        cell.model = self.model.goods.info;
        return cell.height;
    }else {
        return 150;
    }
}

-(void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
